package com.itheima.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.PageResult;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.SysLog;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.utils.ResultUtils;
import com.itheima.vo.CategoryMenuVO;
import com.itheima.service.CategoryMenuService;
import com.itheima.vo.SysLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "菜单信息相关接口",description = "Category Menu Rest Api")
@RestController
@RequestMapping("/categoryMenu")
public class CategoryMenuController {

    @Autowired
    private CategoryMenuService categoryMenuService;

    private  CategoryMenu categoryMenu = new CategoryMenu();

   /* *
    * @Author WeiYue
    * @Description 添加菜单
    * @Date 14:00 2020/8/5
    * @Param
    * @return
    **/

    @ApiOperation(value = "添加菜单")
    @PostMapping("/add")
    public String add(@RequestBody CategoryMenuVO categoryMenuVO){
        //菜单级别
        Integer menuLevel = categoryMenuVO.getMenuLevel();
        categoryMenu.setMenuLevel(menuLevel);
        //图标
        String icon = categoryMenuVO.getIcon();
        categoryMenu.setIcon(icon);
        //是否显示
        categoryMenu.setIsShow(categoryMenuVO.getIsShow());
        //菜单类型
        categoryMenu.setMenuType(categoryMenuVO.getMenuType());
        //菜单名称
        categoryMenu.setName(categoryMenuVO.getName());
        //父ID
        categoryMenu.setParentUid(categoryMenuVO.getParentUid());
        //排序字段，越大越靠前
        categoryMenu.setSort(categoryMenuVO.getSort());
        //状态
        categoryMenu.setStatus(categoryMenuVO.getStatus());
        //uid
        categoryMenu.setUid(categoryMenuVO.getUid());
        //url
        categoryMenu.setUrl(categoryMenuVO.getUrl());
        try {
            categoryMenuService.add(categoryMenu);
            return ResultUtils.result("success",MessageConstant.CATEGORYMENU_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_ADD_FAIL);
        }
    }

    /* *
     * @Author WeiYue
     * @Description 删除菜单
     * @Date 14:21 2020/8/5
     * @Param [categoryMenuVO]
     * @return java.lang.String
     **/
    @ApiOperation(value = "删除菜单")
    @PostMapping("/delete")
    public String delete(@RequestBody CategoryMenuVO categoryMenuVO){
        //菜单级别
        Integer menuLevel = categoryMenuVO.getMenuLevel();
        categoryMenu.setMenuLevel(menuLevel);
        //图标
        String icon = categoryMenuVO.getIcon();
        categoryMenu.setIcon(icon);
        //是否显示
        categoryMenu.setIsShow(categoryMenuVO.getIsShow());
        //菜单类型
        categoryMenu.setMenuType(categoryMenuVO.getMenuType());
        //菜单名称
        categoryMenu.setName(categoryMenuVO.getName());
        //父ID
        categoryMenu.setParentUid(categoryMenuVO.getParentUid());
        //排序字段，越大越靠前
        categoryMenu.setSort(categoryMenuVO.getSort());
        //状态
        categoryMenu.setStatus(categoryMenuVO.getStatus());
        //uid
        categoryMenu.setUid(categoryMenuVO.getUid());
        //url
        categoryMenu.setUrl(categoryMenuVO.getUrl());
        try {
            categoryMenuService.delete(categoryMenu);
            return ResultUtils.result("success",MessageConstant.CATEGORYMENU_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_DELETE_FAIL);
        }
    }

    /* *
     * @Author WeiYue
     * @Description 编辑菜单
     * @Date 14:48 2020/8/5
     * @Param [categoryMenuVO]
     * @return java.lang.String
     **/
    @ApiOperation(value = "编辑菜单")
    @PostMapping("/edit")
    public String edit(@RequestBody CategoryMenuVO categoryMenuVO){
        //菜单级别
        Integer menuLevel = categoryMenuVO.getMenuLevel();
        categoryMenu.setMenuLevel(menuLevel);
        //图标
        String icon = categoryMenuVO.getIcon();
        categoryMenu.setIcon(icon);
        //是否显示
        categoryMenu.setIsShow(categoryMenuVO.getIsShow());
        //菜单类型
        categoryMenu.setMenuType(categoryMenuVO.getMenuType());
        //菜单名称
        categoryMenu.setName(categoryMenuVO.getName());
        //父ID
        categoryMenu.setParentUid(categoryMenuVO.getParentUid());
        //排序字段，越大越靠前
        categoryMenu.setSort(categoryMenuVO.getSort());
        //状态
        categoryMenu.setStatus(categoryMenuVO.getStatus());
        //uid
        categoryMenu.setUid(categoryMenuVO.getUid());
        //url
        categoryMenu.setUrl(categoryMenuVO.getUrl());
        try {
            categoryMenuService.edit(categoryMenu);
            return ResultUtils.result("success",MessageConstant.CATEGORYMENU_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_EDIT_FAIL);
        }
    }

    /* *
     * @Author WeiYue
     * @Description 获取所有菜单列表
     * @Date 15:00 2020/8/5
     * @Param []
     * @return java.lang.String
     **/
    @ApiOperation(value = "获取所有菜单列表")
    @GetMapping("/getAll")
    public String getAll(){
        try {
            List<CategoryMenu> allList = categoryMenuService.getAllList();
            return ResultUtils.result("success",allList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_GETALL_FAIL);
        }
    }

    /* *
     * @Author WeiYue
     * @Description 获取二级菜单-按钮列表
     * @Date 15:18 2020/8/5
     * @Param []
     * @return java.lang.String
     **/
    @ApiOperation(value = "获取二级菜单-按钮列表")
    @GetMapping("/getButtonAll")
    public String getButtonAll(){
        try {
            List<CategoryMenu> buttonAll = categoryMenuService.getButtonAll();
            return ResultUtils.result("success",buttonAll);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_GETBUTTONALL_FAIL);
        }
    }

    /* *
     * @Author WeiYue
     * @Description 获取菜单列表
     * @Date 15:34 2020/8/5
     * @Param [categoryMenuVO]
     * @return java.lang.String
     **/
    @ApiOperation(value = "获取菜单列表")
    @PostMapping("/getList")
    public String getList(@RequestBody(required = false) CategoryMenuVO categoryMenuVO) {
        Page<CategoryMenu> logList = categoryMenuService.findList(categoryMenuVO);
        Map<String, Object> map = new HashMap();
        map.put("records",logList.getResult());
        map.put("total",logList.getTotal());
        map.put("size",logList.getPageSize());
        map.put("current",logList.getPageNum());
        map.put("orders","");
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return ResultUtil.result("success", map);
    }

   /* *
    * @Author WeiYue
    * @Description 置顶菜单
    * @Date 15:54 2020/8/5
    * @Param [categoryMenuVO]
    * @return java.lang.String
    **/
    @ApiOperation(value = "置顶菜单")
    @PostMapping("/stick")
    public String stick(@RequestBody CategoryMenuVO categoryMenuVO){
        String uid = categoryMenuVO.getUid();
        try {
            categoryMenuService.stick(uid);
            return ResultUtils.result("success",MessageConstant.CATEGORYMENU_STICK_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result("error", MessageConstant.CATEGORYMENU_STICK_FAIL);
        }
    }
}
